/**
 * Created by jianglinjie on Tue Oct 10 2017
 * 
 */
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _StorageKeys = require('../constants/StorageKeys');

var StorageKeys = _interopRequireWildcard(_StorageKeys);

var _config = require('../config');

var _config2 = _interopRequireDefault(_config);

var _log = require('../log');

var _log2 = _interopRequireDefault(_log);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var AuthStore = function () {
  function AuthStore() {
    _classCallCheck(this, AuthStore);
  }

  _createClass(AuthStore, [{
    key: 'saveLoginResult',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(result) {
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _config2.default.storage.save({
                  key: StorageKeys.AuthServiceLoginResult,
                  data: result,
                  expires: null
                });

              case 2:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function saveLoginResult(_x) {
        return _ref.apply(this, arguments);
      }

      return saveLoginResult;
    }()
  }, {
    key: 'loadLoginResult',
    value: function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
        var result;
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.prev = 0;
                _context2.next = 3;
                return _config2.default.storage.load({
                  key: StorageKeys.AuthServiceLoginResult
                });

              case 3:
                result = _context2.sent;
                return _context2.abrupt('return', result);

              case 7:
                _context2.prev = 7;
                _context2.t0 = _context2['catch'](0);
                return _context2.abrupt('return', null);

              case 10:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this, [[0, 7]]);
      }));

      function loadLoginResult() {
        return _ref2.apply(this, arguments);
      }

      return loadLoginResult;
    }()
  }, {
    key: 'removeLoginResult',
    value: function () {
      var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
        return regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.prev = 0;
                _context3.next = 3;
                return _config2.default.storage.remove({
                  key: StorageKeys.AuthServiceLoginResult
                });

              case 3:
                _context3.next = 8;
                break;

              case 5:
                _context3.prev = 5;
                _context3.t0 = _context3['catch'](0);

                _log2.default.error('removeLoginResult error %o', _context3.t0);

              case 8:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, this, [[0, 5]]);
      }));

      function removeLoginResult() {
        return _ref3.apply(this, arguments);
      }

      return removeLoginResult;
    }()
  }, {
    key: 'addRecentLoginedUserInfo',
    value: function () {
      var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(userInfo) {
        var preUserInfoList, newUserInfoList, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, preUserInfo;

        return regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.prev = 0;
                _context4.next = 3;
                return this.getRecentLoginedUserInfos();

              case 3:
                preUserInfoList = _context4.sent;
                newUserInfoList = [userInfo];
                _iteratorNormalCompletion = true;
                _didIteratorError = false;
                _iteratorError = undefined;
                _context4.prev = 8;

                for (_iterator = preUserInfoList[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                  preUserInfo = _step.value;

                  if (preUserInfo.uid !== userInfo.uid) {
                    //不重复添加
                    newUserInfoList.push(preUserInfo);
                  }
                }

                _context4.next = 16;
                break;

              case 12:
                _context4.prev = 12;
                _context4.t0 = _context4['catch'](8);
                _didIteratorError = true;
                _iteratorError = _context4.t0;

              case 16:
                _context4.prev = 16;
                _context4.prev = 17;

                if (!_iteratorNormalCompletion && _iterator.return) {
                  _iterator.return();
                }

              case 19:
                _context4.prev = 19;

                if (!_didIteratorError) {
                  _context4.next = 22;
                  break;
                }

                throw _iteratorError;

              case 22:
                return _context4.finish(19);

              case 23:
                return _context4.finish(16);

              case 24:
                _context4.next = 26;
                return _config2.default.storage.save({
                  key: StorageKeys.AuthServiceRecentLoginedUsers,
                  data: newUserInfoList,
                  expires: null
                });

              case 26:
                _context4.next = 31;
                break;

              case 28:
                _context4.prev = 28;
                _context4.t1 = _context4['catch'](0);

                _log2.default.error('loadLoginResult error %o', _context4.t1);

              case 31:
              case 'end':
                return _context4.stop();
            }
          }
        }, _callee4, this, [[0, 28], [8, 12, 16, 24], [17,, 19, 23]]);
      }));

      function addRecentLoginedUserInfo(_x2) {
        return _ref4.apply(this, arguments);
      }

      return addRecentLoginedUserInfo;
    }()
  }, {
    key: 'getRecentLoginedUserInfos',
    value: function () {
      var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
        var userInfos;
        return regeneratorRuntime.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                _context5.prev = 0;
                _context5.next = 3;
                return _config2.default.storage.load({
                  key: StorageKeys.AuthServiceRecentLoginedUsers
                });

              case 3:
                userInfos = _context5.sent;
                return _context5.abrupt('return', userInfos || []);

              case 7:
                _context5.prev = 7;
                _context5.t0 = _context5['catch'](0);
                return _context5.abrupt('return', []);

              case 10:
              case 'end':
                return _context5.stop();
            }
          }
        }, _callee5, this, [[0, 7]]);
      }));

      function getRecentLoginedUserInfos() {
        return _ref5.apply(this, arguments);
      }

      return getRecentLoginedUserInfos;
    }()
  }]);

  return AuthStore;
}();

exports.default = AuthStore;